package com.wl.auth.controller;


import com.wl.auth.service.SysLoginService;
import com.wl.common.core.constant.ConstantSystem;
import com.wl.common.core.web.dto.RestResultDTO;
import com.wl.common.system.support.dto.LoginDTO;
import com.wl.common.system.support.dto.RegisterDTO;
import com.wl.common.system.support.vo.SysUserVO;
import com.wl.system.api.model.LoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: wanglin
 * @date: 2023-10-23 周一
 * @Version: 1.0
 * @Description:
 */
@Tag(name = "登录接口", description = "SysLoginController")
@Validated
@CrossOrigin
@RestController
@RequestMapping(ConstantSystem.PROJECT_SYS_URL_PREFIX + "token")
public class TokenController {

    @Autowired
    private SysLoginService loginService;

    @Operation(summary = "注册")
    @PostMapping(value = "register")
    public RestResultDTO<Void> register(@Validated @RequestBody RegisterDTO dto) {
        loginService.register(dto);
        return RestResultDTO.newSuccess();
    }

    @Operation(summary = "登录")
    @PostMapping(value = "login")
    public RestResultDTO<LoginUser> login(@Validated @RequestBody LoginDTO dto) {
        return loginService.login(dto);
    }

    @Operation(summary = "退出登录")
    @GetMapping("logout")
    public RestResultDTO<Void> logout(HttpServletRequest request) {
        loginService.logout(request);
        return RestResultDTO.newSuccess();
    }

    @Operation(summary = "刷新token")
    @GetMapping("refreshToken")
    public RestResultDTO<LoginUser> refreshToken(HttpServletRequest request) {
        return loginService.refreshToken(request);
    }

    @Operation(summary = "获取用户信息")
    @RequestMapping(value = "getInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<SysUserVO> getInfo(HttpServletRequest request) {
        return RestResultDTO.newSuccess(loginService.getInfo(request));
    }
}

